﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using T.FAS.Runtime.Metadata.Entity;
using T.ServiceComponent.DAC.Service;
using T.ServiceComponent.DataAccessEx.Core;

namespace T.FAS.Runtime.Metadata.Dao
{
    public class MetaDataContentExtDao : DaoService<MetaDataContentExt>
    {
        public MetaDataContentExtDao(string db = "TFAS") : base()
        {
            this.DBName = db;
        }

        public override string IDColumnName
        {
            get
            {
                return "ID";
            }
        }

        public override string TableName
        {
            get
            {
                return "Meta_ContentExt";
            }
        }
        public override List<string> ExceptColumnName
        {
            get 
            {
                return new List<string>() { "MetaDataName" };
            }
        }
        public int DeleteByMetaDataID(string meteDataID, string projectBranchID)
        {
            return this.DB.ExecSqlStatement(string.Format("Delete from {0} where MetaDataID='{1}' AND ProjectBranchID='{2}'",TableName,meteDataID, projectBranchID));
        }

        public List<MetaDataContentExt> GetContentExts(string meteDataID, string sourceProjectBranchID)
        {
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            string sqlCondition = string.Format("ProjectBranchID=@ProjectBranchID AND MetaDataID=@MetaDataID");
            paramsList.Add(this.DB.MakeInParam("@ProjectBranchID", TDbDataType.VarChar, sourceProjectBranchID));
            paramsList.Add(this.DB.MakeInParam("@MetaDataID", TDbDataType.VarChar, meteDataID));            
            var sql = @"SELECT ID,VersionID,MetaDataID,MetaDataCode,MetaTypeID,DataResultType,Creator,CreateTime,LastModifier,LastModifyTime,ActionType,MetaDataType,ProjectBranchID,DBType,DBName,TableName,ParentMetaDataCode,EngineType,ClientType,TemplateID,Thumbnail,BusinessType,MetaExtJSON FROM Meta_ContentExt";
            var ds = this.DB.ExecuteDataSet(string.Format("{0} WHERE {1} ", sql, sqlCondition), paramsList.ToArray());
            List<MetaDataContentExt> list = new List<MetaDataContentExt>();
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    var contentExt = ConvertToObject(this.Columns, row);                    
                    list.Add(contentExt);
                }
            }
            return list;
        }

        public bool IsExistDBTable(string projectBranchID, string dbName, string dbType, string tableName, string meteDataID) {
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            string sqlCondition = "ProjectBranchID=@ProjectBranchID";
            paramsList.Add(this.DB.MakeInParam("@ProjectBranchID", TDbDataType.VarChar, projectBranchID));
            if (string.IsNullOrEmpty(dbName) == false) {
                sqlCondition = string.Format("{0} AND DBName=@DBName", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@DBName", TDbDataType.VarChar, dbName));
            }
            if (string.IsNullOrEmpty(dbType) == false)
            {
                sqlCondition = string.Format("{0} AND DBType=@DBType", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@DBType", TDbDataType.VarChar, dbType));
            }
            if (string.IsNullOrEmpty(tableName) == false)
            {
                sqlCondition = string.Format("{0} AND TableName=@TableName", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@TableName", TDbDataType.VarChar, tableName));
            }
            if (string.IsNullOrEmpty(meteDataID) == false)
            {
                sqlCondition = string.Format("{0} AND MetaDataID=@MetaDataID", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@MetaDataID", TDbDataType.VarChar, meteDataID));
            }
            var sql = @"SELECT 1 FROM Meta_ContentExt";
            var ds = this.DB.ExecuteDataSet(string.Format("{0} WHERE {1} ", sql, sqlCondition), paramsList.ToArray());            
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                return true;
            }
            return false;
        }

        internal object GetMetaDataContentExt(string metaDataId, string projectBranchId, string columnName)
        {
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            string sqlCondition = string.Format("ProjectBranchID=@ProjectBranchID AND MetaDataID=@MetaDataID");
            paramsList.Add(this.DB.MakeInParam("@ProjectBranchID", TDbDataType.VarChar, projectBranchId));
            paramsList.Add(this.DB.MakeInParam("@MetaDataID", TDbDataType.VarChar, metaDataId));

                        
            var sql = string.Format(@"SELECT {0} FROM Meta_ContentExt  WHERE {1}", columnName, sqlCondition);
            var ds = this.DB.ExecuteDataSet(sql, paramsList.ToArray());
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                return ds.Tables[0].Rows[0][columnName];
            }
            return null;
        }

        internal List<MetaDataContentExt> GetMetaDataContentExts(string projectBranchId, string metaTypeId, string metaDataType, string userId)
        {
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            string sqlCondition = "(mc.ProjectBranchID=@ProjectBranchID or mc.ProjectBranchID='Metrics' or mc.ProjectBranchID='Public') AND mc.MetaTypeID=@MetaTypeID";
            paramsList.Add(this.DB.MakeInParam("@ProjectBranchID", TDbDataType.VarChar, projectBranchId));
            paramsList.Add(this.DB.MakeInParam("@MetaTypeID", TDbDataType.VarChar, metaTypeId));
            if (string.IsNullOrEmpty(metaDataType) == false)
            {
                sqlCondition = string.Format("{0} AND mc.MetaDataType=@MetaDataType", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@MetaDataType", TDbDataType.VarChar, metaDataType));
            }
            if (string.IsNullOrEmpty(userId))
            {
                sqlCondition = string.Format("{0} AND mb.MetaDataStatus=1", sqlCondition);
            }
            else
            {
                sqlCondition = string.Format("{0} AND (mb.MetaDataStatus=1 or (mb.MetaDataStatus=0 AND EXISTS (select 1 from Meta_CheckOut checkout where checkout.ProjectBranchID=mc.ProjectBranchID and checkout.MetaDataID=mc.MetaDataID AND checkout.CheckoutState=1 AND checkout.CheckOutUserID=@CheckOutUserID )))", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@CheckOutUserID", TDbDataType.VarChar, userId));
            }

            var sql = string.Format(@"SELECT mc.ID,mc.VersionID,mc.MetaDataID,mc.MetaDataCode,mc.MetaTypeID,mc.DataResultType,mc.Creator,mc.CreateTime,mc.LastModifier,mc.LastModifyTime,mc.ActionType,mc.MetaDataType,mc.ProjectBranchID,mc.DBType,mc.DBName,mc.TableName,mc.ParentMetaDataCode,mc.EngineType,mc.ClientType,mc.TemplateID,mc.Thumbnail,mc.BusinessType,mc.MetaExtJSON,mb.Name MetaDataName FROM {0} mc left join meta_basicinfo mb on mc.ProjectBranchID = mb.ProjectBranchID AND mc.MetaDataID=mb.ID WHERE {1}", TableName, sqlCondition);
            var ds = this.DB.ExecuteDataSet(sql, paramsList.ToArray());
            List<MetaDataContentExt> list = new List<MetaDataContentExt>(); 
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                foreach(DataRow row in ds.Tables[0].Rows)
                {
                    MetaDataContentExt metaDataContentExt = ConvertToObject(Columns,row);
                    metaDataContentExt.MetaDataName = Convert.ToString(row["MetaDataName"]);
                    list.Add(metaDataContentExt);
                }
                
            }
            return list;
        }
    }
}
